# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=hypre
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.23.0
pkgrel=1
pkgdesc='A library for solving large, sparse linear systems on massively parallel computers (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-superlu"
         "${MINGW_PACKAGE_PREFIX}-superlu_dist"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
	 "${MINGW_PACKAGE_PREFIX}-openmp")
makedepends=("${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake")
license=('LGPL')
url="https://github.com/hypre-space/hypre"
source=("https://github.com/hypre-space/hypre/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('8A9F9FB6F65531B77E4C319BF35BFC9D34BF529C36AFE08837F56B635AC052E2')

build() {
  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DHYPRE_WITH_OPENMP=ON \
      -DHYPRE_WITH_DSUPERLU=ON \
      -DTPL_DSUPERLU_LIBRARIES=ON \
      -DTPL_DSUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu_dist \
      -DTPL_DSUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu_dist.a \
      ../${_realname}-${pkgver}/src

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      -DHYPRE_ENABLE_SHARED=ON \
      -DHYPRE_WITH_OPENMP=ON \
      -DHYPRE_WITH_DSUPERLU=ON \
      -DTPL_DSUPERLU_LIBRARIES=ON \
      -DTPL_DSUPERLU_INCLUDE_DIRS=${MINGW_PREFIX}/include/superlu_dist \
      -DTPL_DSUPERLU_LIBRARIES=${MINGW_PREFIX}/lib/libsuperlu_dist.dll.a \
      ../${_realname}-${pkgver}/src

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install

  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYRIGHT ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE 
  
  #Fix: The DLL gets installed to ../lib/libHYPRE.dll instead of ../bin/libHYPRE.dll
  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin
}
